package com.mango.iot.gateway.tcp.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

/**
 * kafka生产者
 *
 * @author liangfeihu
 * @since 2023/7/3 10:01
 */
@Slf4j
@Component
public class KafkaProducer {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    /**
     * 发送消息方法
     */
    public void sendMessage(String topic, Object msg) {
        try {
            kafkaTemplate.send(topic, JSONObject.toJSONString(msg));
            log.info("mqs message topic:{},msg:{}", topic, JSON.toJSONString(msg));
        } catch (Exception e) {
            log.error("mqs send msg error=", e);
        }
    }

}

